package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.concurrent.TimeUnit;

@SpringBootApplication
@EnableDiscoveryClient //开启服务客户端
public class CloudConfigApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext applicationContext = SpringApplication.run(CloudConfigApplication.class, args);

        while(true) {
            //当动态配置刷新时，会更新到 Enviroment中，因此此处每隔一秒从Enviroment中获取配置
            String userName = applicationContext.getEnvironment().getProperty("redis.host");
            String userAge = applicationContext.getEnvironment().getProperty("redis.pwd");
            System.out.println("redis host : " + userName + "; pwd: " + userAge);

            //获取当前部署的环境
            //String currentEnv = applicationContext.getEnvironment().getProperty("current.env");
            //System.err.println("in [ "+currentEnv+" ] enviroment; "+"user name :" + userName + "; age: " + userAge);

            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}